<template>
  <div class="login-wrap">
    <div class="ms-title">权限管理管理系统</div>
    <div class="ms-login">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="0px"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" placeholder="username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="password"
            v-model="ruleForm.password"
            @keyup.enter.native="submitForm('ruleForm')"
          ></el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button
            type="primary"
            v-loading.fullscreen.lock="fullscreenLoading"
            @click="submitForm('ruleForm')" :loading="loading"
          >登录</el-button>
        </div>
        <p v-model="tips" style="font-size:12px;line-height:30px;color:red;">{{tips}}</p>
        
      </el-form>
    </div>
  </div>
</template>

<script>
//import utils from '../utils/index'
import Cookies from "js-cookie";
 
export default {
  data: function() {
    return {
      fullscreenLoading: false,
      loading:false,
      tips: "",
      ruleForm: {
        username: "yh",
        password: "yh"
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      var _this = this;
      _this.loading = true;
      _this.$refs[formName].validate(valid => {
        if (valid) {
          var res = _this.$api
            .login(_this.ruleForm)
            .then(res => {
              if (res.code == 200) {
 
                _this.loading = false;
                _this.$common.setSessionStorage('token', res.data[0].token);
                _this.$common.setSessionStorage('username',res.data[0].username);
              //  Cookies.set("token", res.data[0].token);
                _this.$store.dispatch("add_Menus",res.sysMenuVoList);

                //动态设置路由
                _this.$store.dispatch("add_Routes", res.sysMenuVoList);
               _this.$router.push({ path: "/" });
                // ===============下面是存放storage=======================
                // utils.storage.set('ms_username', this.ruleForm, resources => {
                //   _this.fullscreenLoading = true
                //   _this.$router.push('/index');
                // })
                   return;
              } else {
                 _this.loading = false;
                _this.$message({
                  message: res.msg,
                  type: "error"
                });
              }
            })
            .catch(err => {
              _this.$message({
                message: err,
                type: "error"
              });
            });
        }
      });
    }
  }
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
}
.ms-title {
  position: absolute;
  top: 50%;
  width: 100%;
  margin-top: -230px;
  text-align: center;
  font-size: 30px;
  color: #fff;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 300px;
  height: 160px;
  margin: -150px 0 0 -190px;
  padding: 40px;
  border-radius: 5px;
  background: #fff;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
}
</style>
